package com.youthburin.rmp.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.youthburin.base.domain.Dict;
import com.youthburin.base.services.DictService;
import com.youthburin.rmp.domain.RmpDict;
import com.youthburin.rmp.domain.RmpDictPK;
import com.youthburin.rmp.repositories.RmpDictRepository;

@Service("rmpDictService")
public class RmpDictServiceImpl implements DictService {
	@Autowired
	private RmpDictRepository rmpDictRepository;

	@Override
	@Cacheable("rmp-dict-list-cache")
	public List<Dict> getByType(String type) {
		List<RmpDict> rmpDicts = rmpDictRepository.findByTypeCode(type);
		if (rmpDicts != null && !rmpDicts.isEmpty()) {
			List<Dict> dicts = new ArrayList<Dict>();
			for (RmpDict rmpDict : rmpDicts) {
				Dict dict = new Dict();
				dict.setCode(rmpDict.getId().getCode());
				dict.setName(rmpDict.getName());

				dicts.add(dict);
			}

			return dicts;
		}
		return null;
	}

	@Override
	@Cacheable("rmp-dict-cache")
	public Dict getByTypeAndCode(String type, String code) {
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(code)) {
			RmpDictPK pk = new RmpDictPK();
			pk.setTypeCode(type.toUpperCase());
			pk.setCode(code);
			RmpDict rmpDict = rmpDictRepository.findOne(pk);
			if (rmpDict != null) {
				Dict dict = new Dict();
				dict.setCode(rmpDict.getId().getCode());
				dict.setName(rmpDict.getName());

				return dict;
			}
		}
		return null;

	}

}
